$(function(){
	$('.jianjie').html("");
	var coursePackageId = ym.getQueryStringByName('coursePackageId');
	/*课程包详情*/
	ym.ajax({
		url:"/course/queryCoursePackage",
		param:{ 
			coursePackageId :coursePackageId,
		},
		success:function(data){
			if(data.res == 1){
				$('.kechengb').html("");
				console.log(data);
				var html = "";
				var html2 = "";
				html2 = '<img src="'+data.obj.results[0].attachmentUrl+'">';
				$('.kechengb').append(html2);
				var zhekoujia = data.obj.results[0].discountPrice /100;
				var zongjai = data.obj.results[0].totalPrice /100;
				$('.whitesp').text(data.obj.results[0].coursePackageName);
				$('.jieshu').text(data.obj.results[0].total);
				$('.goumai').text(data.obj.results[0].saleTotalNumber);
				$('.actualprice').text('￥'+zhekoujia);
				$('.doctors').text(data.obj.results[0].lecturer);
				$('.delete').text('￥'+zongjai);
				$('.zjiage').text(zhekoujia);
				$('.jianjie').html(data.obj.results[0].courseDesc);
				if(data.obj.results[0].isBuy == 0){
					html = '<button type="button" class="buy">￥<span class="zjiage">'+zhekoujia+'</span>购买课程</button>';
					$('#foo').html(html);
				}else{
					html = '<button type="button" class="yaozhifu"><span class=""></span>已购买</button>';
					$('#foo').html(html);
				}
			}else{console.log("获取课程包失败!")}
		}
	});
	
	
	/*课程包里的课程*/
	ym.ajax({
		url:'/course/queryCoursePackageDetail',
		param:{coursePackageId:ym.getQueryStringByName('coursePackageId')},
		success:function(data){
			console.log(data);
			if(data.res ==1){
				var html ="";
				for(var i=0;i<data.obj.length;i++){
					var reslt = data.obj[i];
					var time = data.obj[i].createTime;
					time = time.substr('0','10');
					html += '<li class="am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left xxkc">'
					html +='<div class="am-u-sm-3 am-list-thumb">'
					html +=	'<a href="javascript:void(0)" class="">'
					html +=	'<img src="../img/zu7.png" alt="疫苗！"/>'
					html +=	'</a>'
					html +=	'</div>'
					html +=	'<div class=" am-u-sm-8 w72 am-list-main">'
					html +=	'<h3 class="alt twospace" style="margin-top:-2px"><a href="javascript:void(0)" class="">'+reslt.courseName+'</a></h3>'
					html +=	'<p class="learning">'
					html +=	'<span style="display: inline-block;margin-top: 0.3rem">'+time+'</span>'
					if(data.obj[i].trySee == 1){
						html +=	'<span class="floatr colorf8546d mianfei">免费试听</span>';
					}else{
						html +=	'<span class="floatr colorf8546d mianfei"></span>';
					}
					html +=	'<span class="none courseId">'+data.obj[i].courseId+'</span>'
					html +=	'</p>'
					html +=	'</div>'
					html +=	'</li>'
				}	
				$('.am-list').html(html);
			}
			else{console.log("数据获取失败！")}
		}
	});
	/*详细课程*/
	$('body').on('click','.xxkc',function(){
		var zhi = $(this).find('.mianfei').text();
		var buy = $('body').find('.buy');
		if(buy.length > 0){
			if(zhi == "免费试听"){
				var courseId = $(this).find('.courseId').text();
				window.location.href="ship.html?courseId="+courseId;
			}else{
				$("#pays").modal('open');
			}
		}else{
			var courseId = $(this).find('.courseId').text();
			window.location.href="ship.html?courseId="+courseId;
		}
	})
	
	function isCollect(){
   		/*判断是否已收藏*/
		ym.ajax({
			url:'/usercollectid/whetherCollection',
			param:{ 
				collectionTypeId :coursePackageId,
			},
			success:function(data){
				if(data.obj ==1){
					$('.collect-icon').attr('src','../img/dynamic-active-star.png');
				}else{
					$('.collect-icon').attr('src','../img/article-sc.png');
				}
			}
		});
   	}
   	isCollect();
   	/*收藏*/
   	$('#collect').on('click',function(){
   		ym.ajax({
			url:'/usercollectid/collection',
			param:{ 
				collectionType:4,
				collectionTypeId :coursePackageId,
			},
			success:function(data){
				isCollect();
			}
		});
   })
   	/*点击购买*/
   	var urls = window.location.pathname;
   	urls = urls+"?coursePackageId="+coursePackageId;
   	$('body').on('click','.buy',function(){
   		window.location.href ='pay.html?&coursePackageId='+coursePackageId+'&urls='+urls;
   	})
})
